//
//  NRWCarCarDetailShareView.m
//  New_Runway
//
//  Created by 邢毅 on 2018/5/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarCarDetailShareView.h"

@implementation NRWCarCarDetailShareView
{
    UIView *_mainView;
    UIView *_bottomView;
    
    UIButton *photoBtn;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
        self.frame = [UIScreen mainScreen].bounds;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];
        self.showSavePhotoBtn = YES;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    //_mainView 和 canncelBtn 设置的frame先让它在屏幕外面, show的时候再回来
    _mainView = DPView()
    .setBackgroundColor([UIColor whiteColor])
    .setFrame(CGRectMake(0, Screen_height, Screen_width, 145));
    
    _bottomView = DPView()
    .setBackgroundColor([UIColor whiteColor])
    .setFrame(CGRectMake(0, _mainView.viewMaxY, Screen_width, 45 + getSafeBottomMargin()));
    
    UIButton *cancelBtn = DPButton()
    .setNormalTitle(@"取消")
    .setTitleFont(getPingfangFont(17))
    .setNormalTitleColor([UIColor blackColor])
    .addClickAction(self, @selector(cancelBtnDidClick:))
    .setFrame(CGRectMake(0, 0, Screen_width, 45));
    
    UIButton *wxBtn = DPLayoutButton(15, DPBtnLayoutTypeUpImageDownTitle)
    .setNormalImage(getImageFromName(@"share_wx"))
    .setNormalTitle(@"微信")
    .setTitleFont(getPingfangFont(12))
    .setNormalTitleColor(getColorFromString(@"#333333"))
    .setTag(1)
    .addClickAction(self, @selector(shareBtnDidClick:));
    
    UIButton *wxCycle = DPLayoutButton(15, DPBtnLayoutTypeUpImageDownTitle)
    .setNormalImage(getImageFromName(@"share_pyq"))
    .setNormalTitle(@"朋友圈")
    .setTitleFont(getPingfangFont(12))
    .setNormalTitleColor(getColorFromString(@"#333333"))
    .setTag(2)
    .addClickAction(self, @selector(shareBtnDidClick:));
    
    UIButton *qqBtn = DPLayoutButton(15, DPBtnLayoutTypeUpImageDownTitle)
    .setNormalImage(getImageFromName(@"share_qq"))
    .setNormalTitle(@"QQ")
    .setTitleFont(getPingfangFont(12))
    .setNormalTitleColor(getColorFromString(@"#333333"))
    .setTag(3)
    .addClickAction(self, @selector(shareBtnDidClick:));
    
    UIButton *qqCycleBtn = DPLayoutButton(15, DPBtnLayoutTypeUpImageDownTitle)
    .setNormalImage(getImageFromName(@"share_qqkj"))
    .setNormalTitle(@"QQ空间")
    .setTitleFont(getPingfangFont(12))
    .setNormalTitleColor(getColorFromString(@"#333333"))
    .setTag(4)
    .addClickAction(self, @selector(shareBtnDidClick:));
    
    photoBtn = DPLayoutButton(15, DPBtnLayoutTypeUpImageDownTitle)
    .setNormalImage(getImageFromName(@"share_photo"))
    .setNormalTitle(@"图片分享")
    .setTitleFont(getPingfangFont(12))
    .setNormalTitleColor(getColorFromString(@"#333333"))
    .setTag(5)
    .addClickAction(self, @selector(shareBtnDidClick:));
    
    UIView *lineView = DPView().setBackgroundColor(MAIN_LINE_COLOR());
    
    _mainView.addSubview(wxBtn)
    .addSubview(wxCycle)
    .addSubview(qqBtn)
    .addSubview(qqCycleBtn)
    .addSubview(photoBtn)
    .addSubview(lineView);
    
    _bottomView.addSubview(cancelBtn);
    
    self.addSubview(_mainView)
    .addSubview(_bottomView);
    
    NSArray *btnArr = @[wxBtn, wxCycle, photoBtn, qqBtn, qqCycleBtn];
    
    [btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:50 leadSpacing:16 tailSpacing:16];
    
    [btnArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@90);
        make.centerY.equalTo(_mainView.mas_centerY);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_mainView);
        make.bottom.equalTo(_mainView.mas_bottom);
        make.height.equalTo(@0.5);
    }];
    
    qqBtn.hidden = qqCycleBtn.hidden = YES;
}

- (void)setShowSavePhotoBtn:(BOOL)showSavePhotoBtn {
    
    _showSavePhotoBtn = showSavePhotoBtn;
    
    photoBtn.hidden = !showSavePhotoBtn;
}

- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _mainView.frame = CGRectMake(0, Screen_height - 190 - getSafeBottomMargin(), Screen_width, 145);
        _bottomView.frame = CGRectMake(0, Screen_height - 45 - getSafeBottomMargin(), Screen_width, 45 + getSafeBottomMargin());
    }];
}

- (void)dismiss {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
        _mainView.frame = CGRectMake(0, Screen_height , Screen_width, 145);
        _bottomView.frame = CGRectMake(0, Screen_height + 145, Screen_width, 45);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if ([self.delegate respondsToSelector:@selector(carDetailShareViewDidDisAppear:)]) {
            [self.delegate carDetailShareViewDidDisAppear:self];
        }
    }];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    [self dismiss];
}

- (void)cancelBtnDidClick:(UIButton *)sender {
    
    [self dismiss];
}

- (void)shareBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(carDetailShareView:shareBtn:didClickAtIndex:title:)]) {
        
        [self.delegate carDetailShareView:self shareBtn:sender didClickAtIndex:sender.tag - 1 title:sender.titleLabel.text];
    }
}

@end
